package cn.frh.pet.controller;

import cn.frh.basic.exception.BusinessException;
import cn.frh.basic.util.AjaxResult;
import cn.frh.basic.util.LoginContext;
import cn.frh.basic.util.PageList;
import cn.frh.pet.domain.Pet;
import cn.frh.pet.domain.SearchMasterMsg;
import cn.frh.pet.query.PetQuery;
import cn.frh.pet.query.SearchMasterMsgQuery;
import cn.frh.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    @PostMapping
    public PageList<SearchMasterMsg>getPage(@RequestBody SearchMasterMsgQuery query){
        return seachMasterMsgService.queryPage(query);
    }
    @GetMapping
    public List<SearchMasterMsg> queryAll(){
        return seachMasterMsgService.queryAll();
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg,HttpServletRequest request){
        try {
            //获取当前登录人
            seachMasterMsgService.publish(searchMasterMsg, LoginContext.getCurrentLogininfo(request));
            //service需要返回logininfo和token
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }

    ///pending/processed
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.user(query,LoginContext.getCurrentLogininfo(request));
    }
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        query.setState(0);
        return seachMasterMsgService.pending(query,LoginContext.getCurrentLogininfo(request));
    }
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        query.setState(1);
        return seachMasterMsgService.processed(query,LoginContext.getCurrentLogininfo(request));
    }
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            //获取当前登录人
            seachMasterMsgService.handle(pet, LoginContext.getCurrentLogininfo(request));
            //service需要返回logininfo和token
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }

}
